﻿using Domain;
using Domain.Entity;
using Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class JurisdictionRepository : IJurisdictionRepository
    {
        private readonly MyDbContext myDbContext;

        public JurisdictionRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        private List<Jurisdiction> _jurisdiction; // 假设这里存储了所有的PermissionObject对象
        public List<Quan> GetPermissionObjectPID(int rid)
        {
            // Retrieve the relevant jurisdictions based on the role ID  
             _jurisdiction = myDbContext.Jurisdiction
                .Join(myDbContext.RolesJurisdiction,
                      jurisdiction => jurisdiction.JuriID,
                      rolesJurisdiction => rolesJurisdiction.JuriID,
                      (jurisdiction, rolesJurisdiction) => new { jurisdiction, rolesJurisdiction })
                .Where(joined => joined.rolesJurisdiction.RolesID == rid)
                .Select(joined => new Jurisdiction
                {
                    JuriID = joined.jurisdiction.JuriID,
                    JurName = joined.jurisdiction.JurName,
                    GroupID = joined.jurisdiction.GroupID,
                    JurAddress = joined.jurisdiction.JurAddress
                })
                .ToList();

            List<Quan> tree = BuildPermissionObjectTree(0);

            return tree;
        }
        private List<Quan> BuildPermissionObjectTree(int parentId)
        {
            List<Quan> children = _jurisdiction
                .Where(o => o.GroupID == parentId)
                .Select(o => new Quan
                {
                    JuriID = o.JuriID,
                    JurName = o.JurName,
                    GroupID = o.GroupID,
                    JurAddress = o.JurAddress,
                    Children = BuildPermissionObjectTree(o.JuriID) // 递归构建子节点
                })
                .ToList();

            return children;
        }

        /// <summary>
        /// 获取树状图信息
        /// </summary>
        /// <returns></returns>
        public List<ShuZhuangTu> GetShuZhuangTu()
        {
            List<Jurisdiction> po = myDbContext.Jurisdiction.ToList();
            return GetTreeData(po, 0);//获取根节点
        }
        private List<ShuZhuangTu> GetTreeData(List<Jurisdiction> list, int pid)
        {
            List<ShuZhuangTu> trees = new List<ShuZhuangTu>(); //根据pid做数据过滤
            List<Jurisdiction> filteredList = list.Where(e => e.GroupID == pid).ToList();
            foreach (Jurisdiction item in filteredList)
            {
                ShuZhuangTu tree = new ShuZhuangTu()
                {
                    Id = item.JuriID,
                    Name = item.JurName,
                    shuZhuangTu = GetTreeData(list, item.JuriID)
                };
                trees.Add(tree);
            }
            return trees;
        }
    }
}
